package hczc.longcai.com.hccrowdfunding.home.second_invest;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.HomeActivity;
import hczc.longcai.com.hccrowdfunding.home.second_invest.activities.ChooseReturnActivity;
import hczc.longcai.com.hccrowdfunding.home.second_invest.activities.ProjectDetailsActivity;
import hczc.longcai.com.hccrowdfunding.home.second_invest.adapters.InvestClassifyAdapter;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.InvestClassify;

/**
 * 众筹投资fragment
 */
public class    SecondFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener, PopupWindow.OnDismissListener, View.OnTouchListener {

    private static final String TAG = "SecondFragment";

    private static final String[] allClassify = {"全部", "国际", "股权", "债券", "抵押", "艺术", "公益", "其他"};
    private static final String[] deadlineClassify = {"1天", "1个月", "3个月", "6个月", "12个月", "18个月", "24个月", "36个月"};
    private static final String[] defaultcourseClassify = {"默认进程", "众筹中", "将要结束", "成功结束"};

    private static final int[] allClassifyTabs = {R.mipmap.icon_1, R.mipmap.icon_2,
            R.mipmap.icon_3, R.mipmap.icon_4,
            R.mipmap.icon_5, R.mipmap.icon_6,
            R.mipmap.icon_7, R.mipmap.icon_8};

    private static final int[] deadlineClassifyTabs = null;
    private static final int[] defaultcourseClassifyTabs = {R.mipmap.mrjc, R.mipmap.loading,
            R.mipmap.jyjs, R.mipmap.cgjs};

    public static final int LABLE_ALL = 0;
    public static final int LABLE_INTERNATIONAL = 1;
    public static final int LABLE_STOCK = 2;
    public static final int LABLE_BOND = 3;
    public static final int LABLE_PLEDGE = 4;
    public static final int LABLE_ART = 5;
    public static final int LABLE_PUBLIC = 6;
    public static final int LABLE_ELSE = 7;

    private int lable = 0;

    public static final int STATE_ALL = 0;
    public static final int STATE_DEADLINE = 1;
    public static final int STATE_DEFAULT = 2;

    private static int state = 0;

    private RelativeLayout all;
    private RelativeLayout deadline;
    private RelativeLayout defaults;

    private ImageView allPic;
    private ImageView deadlinePic;
    private ImageView defaultpic;

    private InvestClassifyAdapter adapter;

    private ListView listView;
    private View contentView;
    private PopupWindow p;

    private InvestClassify investClassify;
    private List<InvestClassify> list;
    private List<InvestClassify> allClassifyList;
    private List<InvestClassify> delelineClassifyList;
    private List<InvestClassify> defaultClassifyList;

    private TextView all_text;
    private TextView deadline_text;
    private TextView default_text;
    private HomeActivity activity;
//    private View foot_layout;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = ((HomeActivity) getActivity());
        activity.setChangeLable(new HomeActivity.ChangeLable() {
            @Override
            public void changeLable(int i) {
                lable = i;
                all_text.setText(allClassify[lable]);
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            int lable = arguments.getInt("lable");
            this.lable = lable;
        }

    }

    public SecondFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
        initViews(view);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    private void initViews(View view) {
        all = ((RelativeLayout) view.findViewById(R.id.invest_tab_all));
        all.setOnClickListener(this);
        all.setOnTouchListener(this);
        all_text = ((TextView) view.findViewById(R.id.invest_tab_all_text));
        all_text.setText(allClassify[lable]);
        allPic = ((ImageView) view.findViewById(R.id.invest_tab_all_pic));
        deadline = ((RelativeLayout) view.findViewById(R.id.invest_tab_deadline));
        deadline.setOnClickListener(this);
        deadline.setOnTouchListener(this);
        deadline_text = ((TextView) view.findViewById(R.id.invest_tab_deadline_text));
        deadlinePic = ((ImageView) view.findViewById(R.id.invest_tab_deadline_pic));
        defaults = ((RelativeLayout) view.findViewById(R.id.invest_tab_default));
        defaults.setOnClickListener(this);
        defaults.setOnTouchListener(this);
        default_text = ((TextView) view.findViewById(R.id.invest_tab_default_text));
        defaultpic = ((ImageView) view.findViewById(R.id.invest_tab_default_pic));

//        View home = LayoutInflater.from(getActivity()).inflate(R.layout.activity_home, null);
//        foot_layout = home.findViewById(R.id.foot_layout);

        view.findViewById(R.id.tiaozhuan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ProjectDetailsActivity.class));
            }
        });

        view.findViewById(R.id.huibao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ChooseReturnActivity.class));
            }
        });

        initPics();
    }

    private void initPics() {
//        allPic.setImageBitmap(BitmapUtils.getCompBitmap(getResources(),R.drawable.u52,50,50));
//        deadlinePic.setImageBitmap(BitmapUtils.getCompBitmap(getResources(), R.drawable.u52, 50, 50));
//        defaultpic.setImageBitmap(BitmapUtils.getCompBitmap(getResources(), R.drawable.u52, 50, 50));
    }


    @Override
    public void onClick(View view) {

        Log.d(TAG, "*******************" + lable + "**********************");
        int index = view.getId();
        switch (index) {
            case R.id.invest_tab_all:

                if (allClassifyList == null) {
                    if(lable == 0) {
                        allClassifyList = initDatas(allClassify, allClassifyTabs);
                    }else {
                        allClassifyList = allDatas(allClassify,allClassifyTabs);
                    }
                }else {
                    changeSelect(lable,allClassifyList);
                }

                initPopcupWindow(view, allClassifyList);

                if (p.isFocusable()) {
//                    foot_layout.setSelected(true);
                    all_text.setTextColor(Color.parseColor("#eb8111"));
                }
                state = STATE_ALL;
                break;
            case R.id.invest_tab_deadline:
                if (delelineClassifyList == null) {
                    delelineClassifyList = initDatas(deadlineClassify, deadlineClassifyTabs);
                }
                initPopcupWindow(view, delelineClassifyList);
                if (p.isFocusable()) {
//                    foot_layout.setSelected(true);
                    deadline_text.setTextColor(Color.parseColor("#eb8111"));
                }
                state = STATE_DEADLINE;
                break;
            case R.id.invest_tab_default:
                if (defaultClassifyList == null) {
                    defaultClassifyList = initDatas(defaultcourseClassify, defaultcourseClassifyTabs);
                }
                initPopcupWindow(view, defaultClassifyList);
                if (p.isFocusable()) {
//                    foot_layout.setSelected(true);
                    default_text.setTextColor(Color.parseColor("#eb8111"));
                }
                state = STATE_DEFAULT;
                break;
        }
//        Log.d(TAG, "*******************" + state + "**********************");
        adapter.notifyDataSetChanged();
    }

    private List<InvestClassify> allDatas(String[] allClassify, int[] allClassifyTabs) {
        list = new ArrayList<>();
        int length = allClassify.length;
        for (int i = 0; i < length; i++) {
            if (i == lable) {
                investClassify = new InvestClassify(allClassify[i], true, allClassifyTabs[i]);
            } else {
                investClassify = new InvestClassify(allClassify[i], false, allClassifyTabs[i]);
            }
            list.add(investClassify);
        }
        return list;
    }


    private void initPopcupWindow(View view, List<InvestClassify> list) {
        if (contentView == null) {
            contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popcupwindow_list, null);
            BaseApplication.scaleScreenHelper.loadView(((ViewGroup) contentView));
        }
        adapter = new InvestClassifyAdapter(list, getActivity());
        listView = (ListView) contentView.findViewById(R.id.invest_classify_listview);
        listView.setOnItemClickListener(this);
        listView.setAdapter(adapter);
        p = new PopupWindow(contentView, AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT, true);
        p.setOnDismissListener(this);
        p.setBackgroundDrawable(getActivity().getResources().getDrawable(
                R.drawable.back));
        p.showAsDropDown(view);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        List<InvestClassify> list = null;
        lable = i;
        if (state == STATE_ALL) {
            list = allClassifyList;
        } else if (state == STATE_DEADLINE) {
            list = delelineClassifyList;
        } else if (state == STATE_DEFAULT) {
            list = defaultClassifyList;
        }

        changeSelect(i, list);
    }

    private void changeSelect(int i, List<InvestClassify> list) {
        for (int j = 0; j < list.size(); j++) {
            list.get(j).setIsSelect(false);
        }
        list.get(i).setIsSelect(true);
        all_text.setText(allClassify[lable]);
        adapter.notifyDataSetChanged();
        p.dismiss();
    }


    private List<InvestClassify> initDatas(String[] items, int[] tabs) {
//        Log.d(TAG,"**********initDatas**********");
        list = new ArrayList<>();
        int len = items.length;
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                if (tabs != null) {
                    investClassify = new InvestClassify(items[i], true, tabs[i]);
                } else {
                    investClassify = new InvestClassify(items[i], true, 0);
                }
            } else {
                if (tabs != null) {
                    investClassify = new InvestClassify(items[i], false, tabs[i]);
                } else {
                    investClassify = new InvestClassify(items[i], false, 0);
                }
            }
            list.add(investClassify);
        }
        return list;
    }

    @Override
    public void onDismiss() {
//        foot_layout.setSelected(false);
        all_text.setTextColor(Color.parseColor("#a8a8a8"));
        deadline_text.setTextColor(Color.parseColor("#a8a8a8"));
        default_text.setTextColor(Color.parseColor("#a8a8a8"));
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setPressed(true);
        return false;
    }

}
